<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-form-item label="用户" prop="user_id">
                <el-input v-model="user_name" readonly placeholder="请选择用户"></el-input>
            </el-form-item>
            <el-form-item label="任务" prop="task_id">
                <el-input v-model="task_name" readonly placeholder="请选择任务" @click="checkTask"></el-input>
            </el-form-item>
            <el-form-item label="打卡地点" prop="punch_address">
                <el-input v-model="form.punch_address" clearable placeholder="打卡地点"></el-input>
            </el-form-item>
            <el-form-item label="打卡拍照" prop="punch_photo">
                <sc-upload width='100' height='100' v-model="form.punch_photo"></sc-upload>
            </el-form-item>
            <el-form-item label="打卡时间" prop="punch_time">
                <el-date-picker v-model="form.punch_time" type="datetime" placeholder="开始时间" format="YYYY-MM-DD HH:mm:ss" date-format="MMM DD, YYYY" time-format="HH:mm" style="width:100%" />
            </el-form-item>
            <el-form-item label="打卡类型" prop="type">
                <el-select v-model="form.type" placeholder="请选择打卡类型">
                    <el-option label="签到" value="1"></el-option>
                    <el-option label="签退" value="2"></el-option>
                </el-select>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <task-list v-if="dialog.task" ref="taskDialog" @success="taskBack" @closed="dialog.task = false"></task-list>
</template>

<script>
import taskList from '../list/taskList.vue';
export default {
  components: { taskList },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
                user_id:"",
                task_id:"",
                punch_address:"",
                punch_photo:"",
                punch_time: this.moment().format('YYYY-MM-DD HH:mm:ss'),
                type:"",
			},
            user_name: "",
            task_name: "",
			//验证规则
			rules: {
				user_id: [{ required: true, message: '请选择用户' }],
				task_id: [{ required: true, message: '请选择任务' }],
			},
            dialog:{
                task:false,
            },
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.task.record.edit.post(this.form);
					} else {
						res = await this.$API.task.record.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
            this.form.id = data.id
            this.form.project_id = data.project_id
            this.form.pre_sale = data.pre_sale
            this.form.pre_status = data.pre_status
            this.form.photo_files = data.photo_files
            this.date = data.create_time
            this.project_code = data.project.project_code
            this.metting = data.project.metting
            this.banquet_name = data.project.banquet.banquet_name
            this.project_time = data.project.project_time
		},
        checkTask(){
            this.dialog.task = true
            this.$nextTick(() => {
                this.$refs.taskDialog.open('add')
            })
        },
        taskBack(data){
            this.form.task_id = data.id
            this.task_name = data.task_name
        },
	}
}
</script>

<style></style>
